package com.lx.boot.oss.entity;

import com.lx.annotation.Note;
import com.lx.boot.web.valid.Check;
import com.lx.boot.web.valid.CheckValidatorFunctions;
import com.lx.util.LX;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@Data
@Schema(description = "上传文件链接入参")
public class UploadDTO {

    @NotNull(message = "文件流不能为空!")
    @Schema(description = "文件流")
    protected InputStream inputStream;

    @Schema(description = "是否公共读")
    protected boolean publicAcl = false;

    @NotBlank(message = "文件名不能为空!")
    @Schema(description = "文件名")
    protected String fileName;

    @Schema(description = "文件路径(包含文件名) 不填时自动生成路径")
    protected String path;

    @Schema(description = "contentType 不填时根据文件名生成")
    protected String contentType;

    @Note("根据InputStream生成UploadDTO")
    public static UploadDTO of(InputStream inputStream, String fileName) throws IOException {
        UploadDTO uploadDTO = new UploadDTO();
        uploadDTO.inputStream = inputStream;
        uploadDTO.fileName = fileName;
        return uploadDTO;
    }

    @Note("根据MultipartFile生成UploadDTO")
    public static UploadDTO of(MultipartFile file) throws IOException {
        return of(file.getInputStream(),file.getOriginalFilename());
    }

    @Note("根据byte[]生成UploadDTO")
    public static UploadDTO of(byte[] bytes, String fileName) throws IOException {
        return of(new ByteArrayInputStream(bytes),fileName);
    }

    @Note("根据File生成UploadDTO")
    public static UploadDTO of(File file) throws IOException {
        return of(new FileInputStream(file),file.getName());
    }

}
